<template>
	<div class="system-post-container">
		<el-dialog title="新增岗位" v-model="isShowDialog" >
			<el-form ref="addPostFormRef" :model="formData" :rules="formRules" label-width="80px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="岗位名称" prop="postName">
							<el-input v-model="formData.postName" placeholder="请输入岗位名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="岗位编码" prop="postCode">
							<el-input v-model="formData.postCode" placeholder="请输入岗位编码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="岗位状态" prop="status">
							<el-select v-model="formData.status" placeholder="请选择岗位状态" clearable class="w100">
								<el-option label="正常" value="0"></el-option>
								<el-option label="停用" value="1"></el-option>
								<el-option label="删除" value="2"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="岗位排序" prop="orderNum">
							<el-input-number v-model="formData.orderNum" :min="0" :max="999" clearable class="w100"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col>
						<el-form-item label="备注">
							<el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { addPost } from '/@/api/system/post/index';
import { verifyStringCode } from '/@/utils/toolsValidate';
export default {
	name: 'post/add',
	setup(props, context) {
		const addPostFormRef = ref();
		const validPostCode = (rule, value, callback) => {
			if(!value){
				return callback(new Error('岗位编码不能为空'));
			}
			if(!verifyStringCode(value)){
				return callback(new Error('岗位编码格式不正确，只允许字母或数字或下划线组合成5-16个字符'));
			}
			callback();
		}
		const state = reactive({
			isShowDialog: false,
			formData: {
				orderNum : 1
			},
			formRules : {
				status: { required: true, message: '岗位状态不能为空', trigger: 'change' },
				postName: { required: true, message: '岗位名称不能为空', trigger: 'blur' },
				postCode: { required: true, validator: validPostCode, trigger: 'blur' },
				orderNum: { required: true, message: '岗位排序不能为空', trigger: 'blur' }
			}
		});

		// 打开弹窗
		const openDialog = (row?: object) => {
			state.isShowDialog = true;
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			addPostFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 新增
		const onSubmit = () => {
			addPostFormRef.value.validate(valid => {
				if(valid){
					let params = {
						status: state.formData.status,
						postName: state.formData.postName,
						postCode: state.formData.postCode,
						orderNum: state.formData.orderNum,
						remark: state.formData.remark
					}
					addPost(params).then((res) => {
						if(res.code === 0){
							ElMessage.success('保存成功');
							context.emit('submit');
							closeDialog();
						}
					});
				}
			});
		};
		return {
			addPostFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>